• Setting the text properties of buttons —the textFont, textSize, textAlign, and textStyle properties— is currently accomplished via the grace of the message box. You must type a sequence of the set command as follows:
set textFont of button "Sample Button" to Helvetica
set textSize of btn "Sample Button" to 14
set textStyle of btn "Sample Button" to bold
set textAlign of btn "Sample Button" to center
This article describes a script that lets you set the text properties of buttons using the same Text Style dialog box used to set the text properties of fields and paint text. In addition, the script uses some of the new features in HyperCard 1.2.1 , including the within operator and many of the new synonyms.
The script has only two parts, (1) a message called buttonFont that performs most of the work and (2) a function called getButton that returns the name of a button the user chooses with a click.
When you call buttonFont, the basic algorithm is as follows:
• ButtonFont changes the cursor to the Arrow cursor to indicate that you should choose a button. It then waits for you to click the mouse.
• After you click the mouse, buttonFont calls the function getButton, which returns the name of the button you have chosen. If getButton returns the empty string, you did not click a button, so buttonFont exits.
• ButtonFont then gets the current text properties of the chosen button and saves them as default settings.
• Next, buttonFont locks the screen, chooses the Text tool, and sets the text properties to the default values. It then brings up the Text Style dialog box with each text property set to the value of the default settings
(that is, the text properties of the button). You can then use the dialog box to make any changes to the text properties. Note that textHeight is not a property of buttons, so its value is not used by buttonFont.
• After you finish with the Text Style dialog, buttonFont reads the new values of the text properties. It then sets the text properties of the button to these values. Note that if you canceled the Text Style dialog box, the values are just those of the default setting, so the button's text properties are not changed.
• Finally, buttonFont chooses the Browse tool and unlocks the screen.
Since no mouse events are sent while the buttonFont message is running, the function getButton uses HyperCard 1.2.1's new within operator to determine whether you clicked a location on the screen that is within the region of a button:
• getButton takes one argument, the point where you last clicked (the clickLoc).
• It then goes through all the card buttons using within to see if the point is inside the rect of any of these buttons. If so, it returns the name of the card button.
• If getButton did not return, it then goes through all the background buttons using within to determine if the point is inside the rect of any of these buttons. If so, it returns the name of the background button.
• Finally, if getButton did not return, it returns the empty string because you did not click a button.
To get the most use of the buttonFont message, you will probably want to copy it, and getButton, into the script of your Home stack. There are several ways that you might call it. One is to assign it to a function key. For example, putting the following code in your Home stack lets the F7 key invoke the message:
on functionKey whatKey
if whatKey is 7 then
buttonFont
else
--any other function key assignments
end if
end functionKey
If you don't have function keys, you can assign buttonFont to the key command Option-T (to relate it to Command-T, the key command that brings up the Text Style dialog box). Because HyperTalk cannot trap for a single key press, you will have to press Option-T and then the Return key to send the message. This works best if the property Blind Typing from the User Preferences card is set to true so that the message box does not have to be visible when you type Option-T. The following code lets Option-T invoke buttonFont (where Option-T is the character †):
on †
buttonFont
end †
Finally, here are a few things to note about using the buttonFont message:
• Buttons in the following script cannot have more than one textStyle property; for example, they can be bold or condense, but not bold and condense. The Text Style dialog box, however, allows users to choose more than one text style because fields and paint text can have more than one style. If users forget and set more than one style for a button, buttonFont only uses the first (or topmost) style.
• The text properties of buttons do not affect the text that is displayed when a button shows an icon.
• The appearance of a given font depends on the fonts currently installed in the System. For example, if a user sets a button's font to Times but does not have Times in his System, an automatic font substitution will occur.
• With HyperCard 1.2.1, stacks can be write-protected. If a stack is write-protected, none of the choices in the Text Style dialog box are active, so you can't actually change the text properties of the button.
• To cancel the buttonFont message before you actually select a button, just click where there is no button (or use Command-period).